package com.zhuhao.basic.map;

import lombok.Data;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * java-合并两个Map
 * 有点像map.put()操作，从map中获取给定的key，如有值就对新value、老value进行处理，如没有则直接放入
 *
 * @author halk
 * @date 2022/9/29 16:08
 **/
public class MergeDemo {

    private static Map<String, List<People>> map1 = new HashMap<>();
    private static Map<String, List<People>> map2 = new HashMap<>();
    private static List<People> list = new ArrayList<>();
    private static List<People> list2 = new ArrayList<>();

    static {
        People p1 = new People(1, "Henry");
        People p2 = new People(2, "Annie");
        People p3 = new People(3, "John");
        People p4 = new People(4, "George");
        People p5 = new People(5, "Henry");

        list.add(p1);
        list.add(p2);
        list.add(p3);

        list2.add(p1);
        list2.add(p4);
        list2.add(p5);

        map1.put("1", list);
        map2.put("1", list2);
        map2.put("2", list);
    }

    @Test
    public void testMerge() {
        map1.forEach((k, v) -> System.out.println(k + " : " + v));
        map2.forEach((k, v) -> System.out.println(k + " : " + v));

//        map1.computeIfPresent("1", (k, v) -> {
//
//
//            return v;
//        });

        //可以对两个map进行合并操作，相同的key，可以对两个map的value进行合并去重处理
        map1.forEach((k, v) ->
                map2.merge(
                        k,
                        v,
                        (v1, v2) -> {
                            System.out.println(k);
                            System.out.println(v);
                            System.out.println(v1);
                            System.out.println(v2);
                            return Stream.of(v1, v2).flatMap(Collection::stream).distinct().collect(Collectors.toList());
                        }
                )
        );

    }

    public static void main(String[] args) {

        map1.forEach((k, v) -> System.out.println(k + " : " + v));
        System.out.println("----------");
        map2.forEach((k, v) -> System.out.println(k + " : " + v));

        System.out.println("----------");
        System.out.println("----------");


        //可以对两个map进行合并操作，相同的key，可以对两个map的value进行合并去重处理
//        map1.forEach((k, v) ->
//                map2.merge(
//                        k,
//                        v,
//                        (v1, v2) -> {
//                            System.out.println(k);
//                            System.out.println(v);
//                            return Stream.of(v1, v2).flatMap(Collection::stream).distinct().collect(Collectors.toList());
//                        }
//                )
//        );

        //有点像map.put()操作，从map中获取给定的key，如有值就对新value、老value进行处理，如没有则直接放入
        List<People> newList = new ArrayList<>();
        People p6 = new People(6, "Henry");
        newList.add(p6);
        map2.merge(
                "1",
                newList,
                (oldValue, newValue) -> {
                    System.out.println(oldValue);
                    System.out.println(newValue);
                    return Stream.of(oldValue, newValue).flatMap(Collection::stream).distinct().collect(Collectors.toList());
                }
        );
        map2.merge(
                "3",
                newList,
                (oldValue, newValue) -> {
                    System.out.println(oldValue);
                    System.out.println(newValue);
                    return Stream.of(oldValue, newValue).flatMap(Collection::stream).distinct().collect(Collectors.toList());
                }
        );

        map1.forEach((k, v) -> System.out.println(k + " : " + v));
        System.out.println("----------");
        map2.forEach((k, v) -> System.out.println(k + " : " + v));
    }

    @Data
    static class People {
        private Integer id;
        private String name;

        public People(Integer id, String name) {
            this.id = id;
            this.name = name;
        }
    }

}

